3.3 - Linux下實現進程守護

Linux下實現進程守護的詳細實現步驟 (僅建議資深Linux玩家操作)


以下兩種方式可任選其一 (建議選擇一):

一、Supervisor

首先安裝supervisor,已安裝的可以跳過。

# 安裝 supervisor
yum install python-setuptools supervisor -y    //Centos使用
apt install python-setuptools supervisor -y    //Ubuntu、Debian使用

# 初始化全局配置文件
touch /etc/supervisord.conf
echo_supervisord_conf > /etc/supervisord.conf

編輯全局配置文件:

vi /etc/supervisord.conf

將文件底部的[include] 分區注釋符號;刪除,加入新的配置文件包含路徑:

[include]
files = /etc/supervisor/conf/*.conf

創建 GoMinerProxy 應用配置所在文件目錄,并創建打開配置文件:

mkdir -p /etc/supervisor/conf
vi /etc/supervisor/conf/gominerproxy.conf

根據實際情況填寫以下內容并保存:

[program:gominerproxy]
directory=/root/go_miner_proxy
command=/root/go_miner_proxy/GoMinerProxy
autostart=true
autorestart=true
stderr_logfile=/root/go_miner_proxy/log/gominerproxy.err
stdout_logfile=/root/go_miner_proxy/log/gominerproxy.log
environment=CODENATION_ENV=prod

其中以下配置項需要根據實際情況更改:

  • directory: GoMinerProxy主程式所在目錄
  • command: GoMinerProxy主程式絕對路徑
  • stderr_logfile: 錯誤日志路徑
  • stdout_logfile: 通常日志路徑

通過全局配置文件啟動supervisor:

supervisord -c /etc/supervisord.conf

日后你可以通過以下指令管理 GoMinerProxy進程:

# 啟動
supervisorctl start gominerproxy

# 停止
supervisorctl stop gominerproxy

# 查看狀態
supervisorctl status gominerproxy

二、Systemd

# 編輯配置文件
vi /usr/lib/systemd/system/gominerproxy.service

將文件中PATH_TO_GOMINERPROXY 更換為程式所在目錄:

[Unit]
Description=gominerproxy
Documentation=https://github.com/GoMinerProxy/GoMinerProxy/
After=network.target

[Service]
WorkingDirectory=/PATH_TO_GOMINERPROXY
ExecStart=/PATH_TO_GOMINERPROXY/gominerproxy
Restart=on-abnormal
RestartSec=5s
KillMode=mixed

StandardOutput=null
StandardError=syslog

[Install]
WantedBy=multi-user.target

使用如下指令操控服務

# 更新配置
systemctl daemon-reload

# 啟動服務
systemctl start gominerproxy

# 設置開機啟動
systemctl enable gominerproxy

results matching ""

    No results matching ""